using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
 
using GameLibrary;
 
namespace ArkanoidGame
{
   public class FloatText : DynamicElement
   {
       #region Private Members
       private TimeSpan m_timeToLive;
       private float m_fadeIncrement = 1f;
       private float m_currentAlpha = 0f;
       private bool m_liveForever = false;
       #endregion
 
       #region Public Properties
       public string Text { get; set; }
       public bool Alive { get; set; }
       public Font Font { get; set; }
 
       public TimeSpan TimeToLive 
       { 
           get { return m_timeToLive; } 
       }
       #endregion
 
       #region Constuctors
       public FloatText(GamePanel owningGame, string text, Font font, Color color, PointF position, PointF acceleration, TimeSpan timeToLive)
           : base(owningGame, position, new SizeF(0, 0), new PointF(0, 0), acceleration)
       {
           this.Alive = true;
           this.Text = text;
           this.Colour = color;
           this.Font = font;
           m_timeToLive = timeToLive;
 
           m_currentAlpha = (float)Colour.A;
           if (m_timeToLive.TotalSeconds == 0)
           {
               m_liveForever = true;
               m_fadeIncrement = 0;
           }
           else
           {
               m_fadeIncrement = -1f * (float)this.Colour.A / (float)m_timeToLive.TotalSeconds;
           }
       }
       #endregion
 
       #region IGameElement Implementation
       public override void Draw(TimeSpan elapsedTime, Graphics g)
       {
           if (g != null)
           {
               g.DrawString(this.Text, this.Font, new SolidBrush(this.Colour), this.Position);
           }
       }
 
       public override void Update(TimeSpan elapsedTime)
       {
           this.Velocity = this.CalculateNextVelocity(elapsedTime);
           this.Position = this.CalculateNextPosition(elapsedTime);
 
           if (m_liveForever != true)
           {
               m_currentAlpha = (float)Math.Max(0, (m_currentAlpha + m_fadeIncrement * elapsedTime.TotalSeconds));
               m_timeToLive -= elapsedTime;

               if (m_timeToLive.TotalSeconds <= 0 || m_currentAlpha <= 0f)
               {
                   this.Alive = false;
               }
           }
 
           this.Colour = Color.FromArgb(
               (int)m_currentAlpha,
               this.Colour.R,
               this.Colour.G,
               this.Colour.B
           );
       }
       #endregion
   }
}